package cn.chenyumeng.blog.dto;

/**
 * 通用响应
 *
 * @program: blog->Result
 * @description: 通用响应
 * @author: cg
 * @create: 2020-07-17 23:22
 **/
public class CommonResponse<T> {

    private Integer code;
    private String message;
    private T data;

    public static <T> CommonResponse ok(T data) {
        return new CommonResponse(data);
    }

    public static <T> CommonResponse ok() {
        return new CommonResponse(null);
    }

    public static <T> CommonResponse build(ResultCode code, T data) {
        return new CommonResponse(code.getCode(), code.getMessage(), data);
    }

    public static <T> CommonResponse build(ResultCode code) {
        return new CommonResponse(code.getCode(), code.getMessage(), null);
    }

    public CommonResponse(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public CommonResponse(Integer code, String message) {
        this.code = code;
        this.message = message;
        this.data = null;
    }

    public CommonResponse(T data) {
        this.code = ResultCode.SUCCESS.getCode();
        this.message = ResultCode.SUCCESS.getMessage();
        this.data = data;
    }

    public CommonResponse() {

    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "CommonResponse{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
